package yt.kpool.activity.setting;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.personal.FastLookUserDetailActivity;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.ChildAccountAPIKeyBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;
import yt.kpool.util.NetWorkUtil;
import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 * 修改子账户
 */
public class EditAccountActivity extends AppCompatActivity {
    private LoadingDialog loadingDialog;

    @BindView(R.id.edit_remark)
    EditText edit_remark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_account);
        ButterKnife.bind(this);
        initTitle();
    }

    public static void start(Activity mactivity, String ChildAccountId, String ChildAccountName, String ChildAccountRemark) {
        Intent intent = new Intent(mactivity, EditAccountActivity.class);
        intent.putExtra("ChildAccountId", ChildAccountId);
        intent.putExtra("ChildAccountName", ChildAccountName);
        intent.putExtra("ChildAccountRemark", ChildAccountRemark);
        mactivity.startActivity(intent);
    }

    public String getChildAccountId() {
        return getIntent().getStringExtra("ChildAccountId");
    }

    public String getChildAccountName() {
        return getIntent().getStringExtra("ChildAccountName");
    }

    public String getChildAccountRemark() {
        return getIntent().getStringExtra("ChildAccountRemark");
    }

    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle(getChildAccountName());
        titleControl.setRightText("保存", new TitleControl.OnClickRight() {
            @Override
            public void onRight() {
                editAcount();
            }
        });

        loadingDialog = new LoadingDialog(this, 0);
        edit_remark.setText(getChildAccountRemark());
    }

    @OnClick({R.id.ll_onlyread, R.id.ll_profittype})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_onlyread:
                APIKeyListActivity.start(EditAccountActivity.this, getChildAccountId(), getChildAccountName());
                break;
            case R.id.ll_profittype:
                //收益模式
                ProfitTypeActivity.start(EditAccountActivity.this, getChildAccountId(), getChildAccountName());
                break;


        }
    }

    /**
     * 查询APIKey
     */
    private void getdata() {
        if (NetWorkUtil.check(this)) {


            loadingDialog.show();
            NetHelperNew.ChildAccountAPIKey(getChildAccountId(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    loadingDialog.dismiss();
                    Log.i("-->", "查询APIKey：" + data);
                    ChildAccountAPIKeyBean childAccountBean = MyGson.getInstance().fromJson(data, ChildAccountAPIKeyBean.class);
                    if (childAccountBean.getType() == 1) {
                        ChildAccountAPIKeyBean.DataBean data1 = childAccountBean.getData();

                        if ("".equals(data1.getAPIKey())) {
                            //跳转生成Apikey界面
                            CreateOnlyReadActivity.start(EditAccountActivity.this, getChildAccountId(), getChildAccountName());
                        } else {
                            //跳转生成只读
                            OnlyReadActivity.start(EditAccountActivity.this, getChildAccountId(), data1.getRemark(), data1.getCreateTime(), data1.getAPIKey(), data1.getUrl(), getChildAccountName(), "");
                        }


                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(getApplicationContext(), msg);
                }
            });


        }

    }

    private void editAcount() {
        if (NetWorkUtil.check(this)) {
            loadingDialog.show();
            NetHelperNew.UpdateChildAccounRemark(getChildAccountId(), edit_remark.getText().toString().trim(), new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "修改备注子账户：" + data);
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        new ErrorDialog(EditAccountActivity.this, "修改成功").show();
                    } else {

                        new ErrorDialog(EditAccountActivity.this, baseBean.getContent()).show();
                    }
                }

                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(EditAccountActivity.this, msg);
                }
            });

        }

    }
}
